package com.wlf.admin.system.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wlf.admin.common.util.AjaxBean;
import com.wlf.admin.common.dto.CacheKey;
import com.wlf.admin.system.domain.RoleMenu;
import com.wlf.admin.system.domain.SysMenu;
import com.wlf.admin.system.domain.SysRole;
import com.wlf.admin.common.dto.ParamPage;
import com.wlf.admin.system.mapper.SysRoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@CacheConfig(cacheNames = CacheKey.role)
public class SysRoleService extends ServiceImpl<SysRoleMapper, SysRole> {
    public AjaxBean getList(SysRole role, ParamPage paramPage) {
        Page<SysRole> page = this.lambdaQuery()
                .like(StrUtil.isNotBlank(role.getName()), SysRole::getName, role.getName())
                .page(paramPage.init());
        return AjaxBean.getOkPage(page);
    }

    @Cacheable(key = "#id")
    public SysRole get(Long id) {
        return this.getById(id);
    }

    @CacheEvict(key = "#role.id")
    public AjaxBean update(SysRole role) {
        return AjaxBean.getByBool(this.updateById(role));
    }

    public AjaxBean add(SysRole role) {
        return AjaxBean.getByBool(this.save(role));
    }

    /**
     * 根据角色Id获取菜单ID
     */
    public AjaxBean getMenuIdById(Long roleId) {
        List<RoleMenu> list = Db.lambdaQuery(RoleMenu.class).eq(RoleMenu::getRoleId, roleId)
                .list();
        if (list.isEmpty()) {
            return AjaxBean.getOkData(new ArrayList<>());
        }
        List<SysMenu> sysMenus = Db.lambdaQuery(SysMenu.class).in(SysMenu::getId, list.stream().map(RoleMenu::getMenuId).collect(Collectors.toList())).list();

        List<Long> pidList = sysMenus.stream().map(SysMenu::getPid).collect(Collectors.toList());

        return AjaxBean.getOkData(sysMenus.stream().map(SysMenu::getId).filter(id -> !pidList.contains(id)).collect(Collectors.toList()));
    }

    @CacheEvict(cacheNames = CacheKey.power, allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public AjaxBean saveRoleMenu(Long roleId, List<Long> menuIds) {
        Db.lambdaUpdate(RoleMenu.class).eq(RoleMenu::getRoleId, roleId).remove();
        for (Long menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            Db.save(roleMenu);
        }
        return AjaxBean.getOkMsg("更新成功");
    }


    @CacheEvict(key = "#id")
    @Transactional(rollbackFor = Exception.class)
    public AjaxBean del(Long id) {
        Db.lambdaUpdate(RoleMenu.class).eq(RoleMenu::getRoleId, id).remove();
        this.removeById(id);
        return AjaxBean.getOkData("删除成功");
    }
}
